<?php $template = Templates::model()->findByPk($order->template_id); ?>
<?php 
$settings = CJSON::decode($template->settings);
$minqties = CJSON::decode($template->min_quantity);
?>
<h1>Store Visit - Order #<?php echo $order->id?> Details
</h1>
<p>&nbsp;</p>
<form class="form" action="/orders/edit/<?php echo $order->id?>" role="form" method="POST">
	<div id="step1">
		<div class="col-lg-12">
			<div class="row">
				<div class="col-lg-5 col-sm-12">
					<div class="form-group">
						<label class="control-label" for="inputName">Store Name</label>
						<div class="controls">
							<input class="form-control" name="data[store_title]" type="text" id="inputName" value="<?php echo $distributor->title;?>" disabled="disabled">
						</div>
					</div>
					<div class="form-group">
						<label class="control-label" for="inputTemplate">Order Form Used</label>
						<div class="controls">
							<input class="form-control" name="data[template_title]" type="text" id="inputTemplate" value="<?php echo $template->title;?>" disabled="disabled">
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="col-lg-12">
			<div id="products">
				
<script type="text/javascript">
    $(window).ready(function(){
		
		var total = $("#final").html();
		$("#final-input").val(total);
        $('.quantity-input').keyup(function(e){
            var value = $(this).val();
			//console.log(value);
            var minqty1 =$(this).parent('td').parent('tr').attr('data-minqty1'); 
            var minqty2 =$(this).parent('td').parent('tr').attr('data-minqty2');
            var price = $(this).data('price');
            if ( value < minqty1){
				
                $(this).parent('td').parent('tr').find('.total').html((price*value).toFixed(2));
				
                $(this).parent('td').parent('tr').find('.base-price').parent('td').removeClass('text-muted').addClass('text-danger');
                $(this).parent('td').parent('tr').find('.discount1').parent('td').removeClass('text-danger').addClass('text-muted');
                $(this).parent('td').parent('tr').find('.discount1').parent('td').removeClass('text-danger').addClass('text-muted');
            }else if((minqty1<=value && minqty2 == 0) || (minqty1<=value && value<minqty2)){
				var discount1 = parseFloat($(this).parent('td').parent('tr').attr('data-discount1'));
				var price_dis =price - (price*(discount1/1000));
                $(this).parent('td').parent('tr').find('.total').html((price_dis*value).toFixed(2));

                $(this).parent('td').parent('tr').find('.discount1').parent('td').removeClass('text-muted').addClass('text-danger');
                $(this).parent('td').parent('tr').find('.base-price').parent('td').removeClass('text-danger').addClass('text-muted');
                $(this).parent('td').parent('tr').find('.discount2').parent('td').removeClass('text-danger').addClass('text-muted');
			}else if(minqty2<=value){
				var discount2 = parseFloat($(this).parent('td').parent('tr').attr('data-discount2'));
				var price_dis =price - (price*(discount2/1000));
                $(this).parent('td').parent('tr').find('.total').html((price_dis*value).toFixed(2));
                
                $(this).parent('td').parent('tr').find('.discount2').parent('td').removeClass('text-muted').addClass('text-danger');
                $(this).parent('td').parent('tr').find('.base-price').parent('td').removeClass('text-danger').addClass('text-muted');
                $(this).parent('td').parent('tr').find('.discount1').parent('td').removeClass('text-danger').addClass('text-muted');
			}
            var total = 0;
            $('.total').each(function(index){
                total = total + parseFloat($(this).html());
            });
            $('#final').html(total.toFixed(2));
            $('#final-input').val(total.toFixed(2));
        });

        $('.type-select').on('change', function(){
            if ($(this).val() == 1 || $(this).val() == 2){
                $(this).parent('td').parent('tr').find('type-quantity').prop('disabled', false);
            }else{
                $(this).parent('td').parent('tr').find('type-quantity').prop('disabled', true);
            }
        });
    });
</script>
<?php 
if(isset($category)){
    $tot = 0;
    foreach($category as $key){ 
		if(!empty($settings)){
			$current_discount=array();
			foreach($settings as $setting => $value_setting){
				if($setting == $key['id'])
					$current_discount =$value_setting;
			}
		}
		//var_dump($current_discount);
		if(!empty($minqties)){
			$current_minqty=array();
			foreach($minqties as $minqty => $value_minqty){
				if($minqty == $key['id'])
					$current_minqty =$value_minqty;
			}
		}
		//var_dump($current_minqty);
	?>
        <h3><?php echo $key['title'] ?></h3>
        <table class="table table-striped table-hover" id="order">
                <tr>
                    <th>Photo</th>
                    <th>Title</th>
                    <th class="text-center" style="width: 70px">Promo<br/><small style="font-size: 9px;" class="text-muted">units</small></th>
                    <th class="text-center" style="width: 70px">Pickup<br/><small style="font-size: 9px;" class="text-muted">units</small></th>
                    <th class="text-center" style="width: 70px">Order<br/><small style="font-size: 9px;" class="text-muted">inner qty</small></th>
                    <th>Base Price</th>
                    <th>Discount 1<br/>
						<?php  if(isset($current_minqty[0])){?>
						<span class="minqty-value1" style="font-size: 9px" class="text-muted"><?php echo $current_minqty[0];?></span> = <span class="discount-value1" data-discount="<?php echo $current_discount[0]/10?>" style="font-size: 9px" class="text-muted"><?php echo $current_discount[0]/10?>%</span>
						<?php }else{?>
						<span class="discount-value1" style="font-size: 9px" class="text-muted">0%</span>
						<?php }?>
					</th>
                    <th>Discount 2<br/>
						<?php  if(isset($current_minqty[1])){?>
						<span class="minqty-value2" style="font-size: 9px" class="text-muted"><?php echo $current_minqty[1];?></span> = <span class="discount-value1" data-discount="<?php echo $current_discount[1]/10?>" style="font-size: 9px" class="text-muted"><?php echo $current_discount[1]/10?>%</span>
						<?php }else{?>
						<span class="discount-value2" style="font-size: 9px" class="text-muted">0%</span>
						<?php }?>
					</th>
                    <th class="text-right">Total</th>
                </tr>
                <?php
                    $sql_tmp = 'SELECT * FROM products WHERE  category_id = '. $key['id'].' and id IN ('. implode(", ",$result). ')' ;
                    $product_category = Yii::app()->db->createCommand($sql_tmp)->queryAll();
                    
                    if(isset($product_category)){
                        
                        foreach($product_category as $item){ 
                           $order_quantity = OrdersQuantity::model()->findByAttributes(array('product_id'=>$item['id'],'order_id'=>$order->id)); 
						   $minqty1 = 0;
						   $minqty2 = 0;
						   $discount1 = 0;
						   $discount2 = 0;
						   if(isset($current_minqty[0])) $minqty1 = $current_minqty[0];
						   if(isset($current_minqty[1])) $minqty2 = $current_minqty[1];
						   if(isset($current_discount[0])) $discount1 = $current_discount[0];
						   if(isset($current_discount[1])) $discount2 = $current_discount[1];
						   
                           ?>
                            <tr data-minqty1="<?php echo $minqty1;?>" data-minqty2="<?php echo $minqty2;?>" data-discount1="<?php echo $discount1;?>" data-discount2="<?php echo $discount2;?>">
                                <td><img width="32px" height="32px" src="/public/products/<?php echo $item['photo']?>"/></td>
                                <td><?php echo $item['title']?></td>
                                <td class="text-center"><input value="<?php if($order_quantity->promo !=0 ){ echo $order_quantity->promo; }else { echo '';}  ?>" name="promo[<?php echo $item['id']?>]" style="width:50px" type="text" class="form-control text-center promo-quantity"/></td>
                                <td class="text-center"><input value="<?php if($order_quantity->pickup !=0 ){ echo $order_quantity->pickup;}else{ echo ''; } ?>" name="pickup[<?php echo $item['id']?>]" style="width:50px" type="text" class="form-control text-center pickup-quantity"/></td>
                                <td class="text-center"><input data-price="<?php echo $item['price']?>" value="<?php  if ($order_quantity->quantity != 0) { echo $order_quantity->quantity ; }else{ echo ''; } ?>" name="products[<?php echo $item['id']?>]" style="width:50px" type="text" class="form-control text-center quantity-input"/></td>
                                <td <?php if( $order_quantity->quantity < 2 ){ ?> class="text-danger" <?php }else{ ?>  class="text-muted"  <?php }?> >$<span class="base-price"><?php echo $item['price']?></span></td>
                                <?php
                                    $color1="";
                                    $color2="";
                                    if(isset($current_minqty[0])){ 
										if(isset($current_minqty[1])){
											$min1 =$current_minqty[0];
											$min2 =$current_minqty[1];
											if($min1<=$order_quantity->quantity && $order_quantity->quantity<$min2){
												$color1="text-danger";
												$color2="text-muted";
											}else if($order_quantity->quantity>$min2){
												$color2="text-danger";
												$color1="text-muted";
											}
										}else{
											$color1="text-danger";
											$color2="text-muted";
										}
                                    }
                                     
                                ?>
                                
                                <td class="<?php echo $color1;?>">
									<?php if(isset($current_discount[0])) { ?>
										$<span class="discount1"><?php echo round(($item['price'] - ($item['price'] * ($current_discount[0]/1000))),2);?></span><br/>
									<?php } ?>
								</td>
                                
                                <td class="<?php echo $color2;?>">
									<?php if(isset($current_discount[1])) { ?>
										$<span class="discount2"><?php  echo round(($item['price'] - ($item['price'] * ($current_discount[1]/1000))),2);?></span><br/>
									<?php } ?>
								</td>
                                 
                                <?php
								
									if(isset($current_minqty[0])){
										
										if($order_quantity->quantity < $current_minqty[0]){
											$price = $order_quantity->quantity * $item['price'];
											//var_dump($price);
										}else {
											if(isset($current_minqty[1])){
												//var_dump($current_minqty[1]);
												if($current_minqty[0] <= $order_quantity->quantity && $order_quantity->quantity < $current_minqty[1]){
													$price = round($order_quantity->quantity * ($item['price'] - ($current_discount[0]/1000)*$item['price']),2);
													//var_dump($price);
												}else if($current_minqty[1] <= $order_quantity->quantity){
													$price = round($order_quantity->quantity * ($item['price'] - ($current_discount[1]/1000)*$item['price']),2);
													//var_dump($price);
												}
											}else if ( $current_minqty[0]<= $order_quantity->quantity){
												$price = round($order_quantity->quantity * ($item['price'] - ($current_discount[0]/1000)*$item['price']),2);
												//var_dump($price);
											}
										}
									}
									
									$tot+= $price;
									
								?>
                                <td class="text-right">$<span class="total"><?php echo $price ; ?></span></td>
                                
                            </tr>
                            
                       <?php  } ?> </table>
                   <?php   }
                   
              } 
           }?>
        
            <table class="table table-striped table-hover">
                 <tr>
                 <td style="width: 80%;"> </td>
                    <td class="text-right" style="width: 20%;">
                        <strong>Total: $<span id="final"><?php echo $tot; ?></span></strong>
                        <input type="hidden" name="data[final]" id="final-input"/>
                    </td>
                </tr>
            </table>
					<p><button type="submit" name="action" value="edit_order" class="btn btn-default">Save change</button>

                    <a href="/orders/view/<?php echo $order->id ?>" type="submit" class="btn btn-success">Cancel</a></p>
			</div>
	  </div>
  </div>
</form>